package com.yz.newspaper.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yz.newspaper.pojo.po.Duty;
import com.yz.newspaper.pojo.result.Result;
import com.yz.newspaper.pojo.unitPo.PageBean;
import com.yz.newspaper.service.IDutyService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;

/**
 * 值班表 前端控制器
 *
 * @author author
 * @since 2024-04-29
 */
@RestController
@RequestMapping("/duty")
@Slf4j
public class DutyController {

    @Autowired
    private IDutyService dutyService;

    @GetMapping("/list")
    @ApiOperation("获取值班表列表")
    public Result getDutyList(String username, Integer pageNum, Integer pageSize) {
        log.info("username:{},pageNum:{},pageSize:{}", username, pageNum, pageSize);
        PageBean pageBean = dutyService.getDutyList(username, pageNum, pageSize);
        return Result.success(pageBean);
    }

    @GetMapping("/byUserId/{userId}")
    @ApiOperation("获取个人值班表")
    public Result getDutyOne(@PathVariable("userId") String userId) {
        log.info("id:{}", userId);
        LambdaQueryWrapper<Duty> queryWrapper = new LambdaQueryWrapper<Duty>().eq(Duty::getDutyUserId, Long.valueOf(userId));
        List<Duty> duties = dutyService.list(queryWrapper);
        return Result.success(duties);
    }

    @GetMapping("/byDutyId/{id}")
    @ApiOperation("根据id获取值班表")
    public Result getDutyById(@PathVariable("id") String id) {
        log.info("id:{}", id);
        Duty duty = dutyService.getById(Long.valueOf(id));
        return Result.success(duty);
    }

    @ApiOperation("添加值班用户")
    @PostMapping
    public Result addDuty(@RequestBody Duty duty) {
        log.info("duty:{}", duty);
        duty.setCreateTime(LocalDateTime.now());
        duty.setDutyScore(9);
        LocalDate endTime = duty.getEndTime();
        LocalDate startTime = duty.getStartTime();
        long dutyDays = ChronoUnit.DAYS.between(startTime, endTime);
        Integer dutyDaysInt = (int) dutyDays; // 将long类型转换为int
        duty.setDutyDays(dutyDaysInt);
        boolean save = dutyService.save(duty);
        return Result.success(save);
    }

    @ApiOperation("修改值班用户")
    @PutMapping
    public Result updateDuty(@RequestBody Duty duty) {
        log.info("duty:{}", duty);
        duty.setCreateTime(LocalDateTime.now());
        LocalDate endTime = duty.getEndTime();
        LocalDate startTime = duty.getStartTime();
        long dutyDays = ChronoUnit.DAYS.between(startTime, endTime);
        Integer dutyDaysInt = (int) dutyDays; // 将long类型转换为int
        duty.setDutyDays(dutyDaysInt);
        boolean update = dutyService.updateById(duty);
        return Result.success(update);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除值班用户")
    public Result deleteDuty(@PathVariable Integer id) {
        log.info("id:{}", id);
        boolean remove = dutyService.removeById(id);
        return Result.success(remove);
    }
}
